<template>
	<f-popup ref="popup" width="600rpx">
		<view class="content">
			<view class="popup-header">
				<view class="title">{{info.title}}</view>
			</view>
			<view class="popup-body">
				<view class="hint">您选择了{{info.title}}无人机</view>
				<view class="hint">必须选择以下产品</view>
				<scroll-view scroll-y class="scroll">
					<view class="radio">
						<view class="radio-option" :class="{'active-radio': activeRadio === item.gid}" v-for="item in info.relation" :key="item.gid" @click="changeRadio(item)">
							<view class="">
								<view class="">{{item.title}}</view>
								<view class="price">
									<text class="price-symbol">¥</text>
									<text class="price-num">{{item.selling_price ? item.selling_price : 0}}</text>
								</view>
							</view>
							<view v-if="activeRadio === item.gid" class="iconfont icon-check"></view>
							<view v-else class="iconfont icon-radio"></view>
						</view>
					</view>
				</scroll-view>
				<view class="flex align-center justify-between">
					<view class="title">数量</view>
					<view class="com-input-number">
						<view class="iconfont icon-jian1" @click="minus"></view>
						<input type="number" :value="internalValue" class="f-input" placeholder=" " @input="changeYear" />
						<view class="iconfont icon-tianjia_huaban" @click="plus"></view>
					</view>
				</view>
			</view>
			<view class="popup-footer">
				<button class="popup-cancel-btn flex-1" @click="cancel">取消</button>
				<button class="popup-confirm-btn flex-1" @click="confirm">确认</button>
			</view>
		</view>
	</f-popup>
</template>

<script>
	export default {
		name: 'specs-popup',
		data() {
			return {
				info: {},
				activeRadio: [],
				internalValue: 1
			}
		},
		methods: {
			// 减
			minus() {
				if (this.internalValue > 1) {
					this.internalValue--
					this.$emit('input', this.internalValue);
				}
			},
			// 加
			plus() {
				this.internalValue++
				this.$emit('input', this.internalValue);
			},
			changeYear(event) {
				const value = Math.abs(event.detail.value);
				if (isNaN(value)) {
					this.$nextTick(() => {
						this.internalValue = 1;
						this.$forceUpdate()
					})
					this.$emit('input', this.internalValue);
					return
				}
				if (value < 1) {
					this.$nextTick(() => {
						this.internalValue = 1;
						this.$forceUpdate()
					})
				} else {
					this.$nextTick(() => {
						this.internalValue = value
						this.$forceUpdate()
					})
				}
				this.$emit('input', this.internalValue);
			},
			// 确定
			confirm() {
				if (!this.activeRadio) {
					uni.showToast({
						icon: 'none',
						title: '请选择产品'
					})
					return
				}
				const params = {
					product_gid: this.info.gid,
					quantity: this.internalValue,
					item_product_gid: this.activeRadio,
					issave: 1
				}
				this.$api.agentProductAddApi(params).then(res=> {
					console.log('res', res);
					uni.showToast({
						title: '添加成功'
					})
					
					this.$emit('confirm', this.info, this.activeRadio)
					this.close()
				}).catch(err=> {
					console.log('err', err);
				})
			},
			// 取消
			cancel() {
				this.$emit('cancel',  this.info)
				this.close()
			},
			changeRadio(item) {
				this.activeRadio = item.gid
				
				// if (this.activeRadio.includes(item.gid)) {
				// 	this.activeRadio = this.activeRadio.filter(value => value !== item.gid)
				// } else {
				// 	this.activeRadio.push(item.gid)
				// }
			},
			open(item) {
				this.info = item
				this.internalValue = 1
				this.activeRadio = item.relation[0].gid
				this.$refs.popup.open()
			},
			close() {
				this.$refs.popup.close()
			},
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		overflow: hidden;
		border-radius: 24rpx;
		background-color: #FFFFFF;
		
		.popup-header {
			padding-top: 64rpx;
			padding-bottom: 16rpx;
			
			.title {
				color: #000000;
				font-size: 36rpx;
				font-weight: 600;
				line-height: 52rpx;
				text-align: center;
			}
		}
		
		.popup-body {
			padding: 0 48rpx;
			padding-bottom: 48rpx;
			
			.title {
				font-size: 32rpx;
				font-weight: 400;
				text-align: left;
				line-height: 48rpx;
				color: rgba(0, 0, 0, 0.9);
			}
			
			.hint {
				font-size: 32rpx;
				font-weight: 400;
				line-height: 48rpx;
				text-align: center;
				color: rgba(0, 0, 0, 0.6);
			}
		}
		
		.price {
			display: flex;
			padding-top: 6rpx;
			align-items: flex-end;
		}
		
		.price-symbol {
			font-size: 22rpx;
			font-weight: 600;
			line-height: 32rpx;
			text-align: left;
		}
		
		.price-num {
			font-size: 28rpx;
			font-weight: 700;
			line-height: 32rpx;
			text-align: left;
		}
		
		.price-unit {
			font-size: 14rpx;
			font-weight: 600;
			line-height: 24rpx;
			text-align: left;
			color: rgba(0, 0, 0, 0.5);
		}
		
		.com-input-number {
			display: flex;
			align-items: center;
			gap: 8rpx;
			
			.f-input {
				width: 50rpx;
				text-align: center;
				font-size: 32rpx;
				color: #000000;
				flex-shrink: 0;
			}
			
			.iconfont {
				width: 34rpx;
				height: 34rpx;
				flex-shrink: 0;
				color: #000000;
				font-size: 32rpx;
				border-radius: 50%;
				border: 1px solid #000000;
				
				display: flex;
				align-items: center;
				justify-content: center;
			}
			
			.icon-tianjia_huaban {
				font-weight: 600;
			}
		}
		
		.scroll {
			max-height: 580rpx;
			padding-top: 40rpx;
			padding-bottom: 32rpx;
		}
		
		.radio {
			
			.radio-option {
				display: flex;
				align-items: center;
				justify-content: space-between;
				border-bottom: 1px solid rgba(0, 0, 0, 0.1);
				
				height: 112rpx;
				font-size: 32rpx;
				font-weight: 400;
				text-align: left;
				line-height: 48rpx;
				color: rgba(0, 0, 0, 0.9);
				
				.iconfont {
					color: #DCDCDC;
					font-size: 42rpx;
				}
			}
			
			.active-radio {
				.iconfont {
					color: var(--primary-color);
				}
			}
		}
	
		.popup-footer {
			display: flex;
			align-items: center;
			border: 1px solid rgba(0, 0, 0, 0.1);
			
			.popup-cancel, .popup-confirm {
				flex: 1;
				display: flex;
				height: 112rpx;
				border-radius: 0;
				align-items: center;
				justify-content: center;
				
				font-size: 32rpx;
				font-weight: 600;
				line-height: 48rpx;
				text-align: center;
				color: rgba(0, 0, 0, 0.9);
			}
			
			.popup-cancel {
				border-right: 1px solid rgba(0, 0, 0, 0.1);
			}
			
			.popup-confirm {
				color: var(--primary-color);
			}
		}
	}
</style>